/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/9/22 10:31
 * @Copyright
 */
package Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapTest3 {
    public static void main(String[] args) {
        //创建Map集合
        Map<String,Integer> buy=new HashMap<>();
        buy.put("苹果手机",2);
        buy.put("智能手表",1);
        buy.put("Java书",1);
        buy.put("C语言书",1);
        buy.put("西瓜",2);
        //map集合的遍历
        //先把所有的键取出来
        Set<String> keySet=buy.keySet();
        //开始根据键找值
        for (String key:keySet){
            Integer value=buy.get(key);
            System.out.println(key+"    "+value);
        }
        System.out.println("-----------------------------");
        //第二种 entrySet()把Map集合转化成Set集合的形式
        Set<Map.Entry<String,Integer>> entrySet=buy.entrySet();
        for (Map.Entry<String,Integer> entry:entrySet){
            String key=entry.getKey();
            Integer value=entry.getValue();
            System.out.println(key+"       "+value);
        }
        System.out.println("------------------------------");
        //迭代器
        Set<String> set=buy.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            String key = it.next();
            Integer value = buy.get(key);
            System.out.println(key+"      "+value);
        }
        System.out.println("-------------------------------");
        Set<Map.Entry<String,Integer>> entrySet2=buy.entrySet();
        Iterator<Map.Entry<String, Integer>> it2 = entrySet2.iterator();
        while (it.hasNext()){
            Map.Entry<String,Integer> entry=it2.next();
            System.out.println(entry.getKey()+"    "+entry.getValue());
        }
        //Lamda
        buy.forEach((k,v)->{
            System.out.println(k+"   "+v);
        });
    }
}
